/**
RoomBot, a IRC bot.
Copyright (C) 2009 Joshua Lückers

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package com.googlecode.roombot.irc;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Thism will deal with all messages received from the IRC server.
 * @author Joshua Lückers
 * @version 17-11-2009
 */
public class MessageParser {
    private static MessageParser messageParser;
    private String source, cmd, target, param;

    static {
        messageParser = new MessageParser();
    }

    /**
     * This will parse the IRC Message.
     * @param connection The connection where the message comes from
     * @param line The message
     */
    public void parse(IRCConnection connection, String line) {
        System.out.println("RAW: " + line);
        if (line.startsWith(":")) {
            //Pattern pattern = Pattern.compile("([:@]([^\\s]+) )?([^\\s]+)( (([^:\\s][^\\s]* ?)*))?( ?:(.*))?");
            Pattern pattern = Pattern.compile("^(?:[:@]([^\\s]+) )?([^\\s]+)(?: ((?:[^:\\s][^\\s]* ?)*))?(?: ?:(.*))?$");
            Matcher matcher = pattern.matcher(line.subSequence(0, line.length()));
            
            if (matcher.matches()) {
                //i.e irc.mibbit.net
                source = matcher.group(1);
                //i.e 433/NOTICE
                cmd = matcher.group(2);
                //i.e RoomBot/#mibbit
                target = matcher.group(3);
                //i.e I have 3093 clients and 1 servers
                param = matcher.group(4);

                System.out.println(cmd);

                //If cmd.lenth() is 3 it is a raw numeric.
                //I should probably move this to i.e a RawNumericParser.
                if (cmd.length() == 3) {
                    int raw = Integer.parseInt(cmd);
                    switch (raw) {
                        case 433:
                            connection.useAlternativeNick();
                            break;
                    }
                }
                else if (cmd.equals("MODE")) {

                }
                else if (cmd.equals("NOTICE")) {
                    
                }
                else if (cmd.equals("PRIVMSG")) {
                    
                }
                else if (cmd.equals("NICK")) {
                    
                }
            }
        }
        else if (line.startsWith("PING")) {
            String str = line.trim();
            String[] parts = str.split(":");
            connection.send("PONG " + parts[1]);
        }
    }

    /**
     * Get the MessageParser instance.
     * @return messageParser The MessageParser instance
     */
    public static MessageParser getInstance() {
        return messageParser;
    }
}
